<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<html>
	<head>
		<title>Successful Upload</title>
	</head>
	
	<body>
		<p>Uploaded Files:</p>
		<%
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key accountKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		Query query = new Query("Audio", accountKey).addSort("mediaName", Query.SortDirection.ASCENDING);
		List<Entity> uploadedAudioFiles = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(15));
		if (uploadedAudioFiles.isEmpty()) {
			%><p>There are no uploaded audio files</p>
			<a href="/upload.jsp">Back to upload</a>
			<%
		} else {
			%>
			<table border="1">
				<tr>
					<th>MediaName</th>
					<th>MediaType</th>
					<th>Blob Key</th>
					<th>Delete File</th>
					<th>Download File</th>
				<tr>
			<%
			for (Entity file: uploadedAudioFiles) {
				String keyParameter = KeyFactory.keyToString(file.getKey());
				%> 
				<tr>
					<td><%= file.getProperty("mediaName") %></td>
					<td><%= file.getProperty("mediaType") %></td>
					<td><%= file.getProperty("blobKey") %></td>
					<td><a href="/delete?entityKey=<%= keyParameter %>">Delete File</a></td>
					<td><a href="/download?entityKey=<%= keyParameter %>">Download File</a></td>
				</tr>
				<%
			}
			%> 
			</table>
			<a href="/upload.jsp">Back to upload</a>
			<%
		}
		%>
	</body>
</html>